"""
获取问财选股数据:筹码集中,股东人数减少10%以上，连续放量,均价上升
"""
from datetime import datetime
import time

import pandas as pd
from DrissionPage import ChromiumPage
from DrissionPage import WebPage
from DrissionPage._units.listener import DataPacket
from DrissionPage.errors import ElementNotFoundError

from x01_stock.xx_util.DBUtil import SqlHelper
from zzz_finish.zz_final.zz_xuangu.wencai_util import handle_biaoxian, handle_jishu, handle_guzhi

page = ChromiumPage()
page.listen.start('https://www.iwencai.com/customized/chart/get-robot-data')

# page.set.headers(header)  # 加入header则无法利用原来浏览器中的cookies
# 需要点击页面的查询放大镜才有数据
# page.get('https://www.iwencai.com/unifiedwap/result?w=%E7%AD%B9%E7%A0%81%E9%9B%86%E4%B8%AD%E5%8D%95%E5%B3%B0,%E8%82%A1%E4%B8%9C%E4%BA%BA%E6%95%B0%E5%87%8F%E5%B0%9110%25%E4%BB%A5%E4%B8%8A%EF%BC%8C%E8%BF%9E%E7%BB%AD%E6%94%BE%E9%87%8F,&querytype=stock&addSign=1722751831804')

page.get(
    'https://www.iwencai.com/unifiedwap/result?w=%E7%AD%B9%E7%A0%81%E9%9B%86%E4%B8%AD,%E8%82%A1%E4%B8%9C%E4%BA%BA%E6%95%B0%E5%87%8F%E5%B0%9110%25%E4%BB%A5%E4%B8%8A%EF%BC%8C%E8%BF%9E%E7%BB%AD%E6%94%BE%E9%87%8F,%E5%9D%87%E4%BB%B7%E4%B8%8A%E5%8D%87&querytype=stock')
time.sleep(3)
try:  # header = res.request.postData
    stat_date = datetime.now().date().strftime('%Y-%m-%d')
    res = page.listen.wait()  # 等待并获取一个数据包
    # print(res.response.body)
    data1 = res.response.body['data']['answer'][0]['txt'][0]['content']['components'][0]['data']['datas']
    #
    # 第一个查询：相关   插入问财返回的基本信息
    for data in data1:
        data = dict(sorted(data.items(), key=lambda item: item[0]))
        print(data)
        sql = (
            f"INSERT INTO `z_sproot_series`.`stock_wencai_choumajizhong` (trade_date,chengbenshangxian90pct,chengbenxiaxiang90pct,"
            f"mairuxinhao,jujia_hbzz,pingjunchengben,jishuxingtai,shoupanhuoli,stock_price,chg_pct,gudong_hbzzl,stock_code ,stock_name,fangliang,"
            f"jizhongdu70pct,jizhongdu90pct,jzd90_paiming) values ('{stat_date}',")
        for key, value in data.items():
            print(key + ':' + str(value))
            if key != 'market_code' and key != 'code':
                print(type(value))
                if type(value) == str:
                    sql = sql + '"' + str(value) + '",'
                else:
                    sql = sql + str(value) + ','
        sql = sql[:-1] + ');'
        print('--------------------------------')
        print(sql)
        SqlHelper.get_instance().execute_curd(sql)
    print('点击获取表现的数据')



    # 【第二个查询】
    page.ele('@text()=表现').click()
    time.sleep(3)
    res2 = page.listen.wait()
    data2 = res2.response.body['data']['answer'][0]['txt'][0]['content']['components'][0]['data']['datas']
    handle_biaoxian(data2,stat_date)


    # 【 第三个查询 技术】
    page.ele('@text()=技术').click()
    time.sleep(3)
    res3 = page.listen.wait()
    data3 = res3.response.body['data']['answer'][0]['txt'][0]['content']['components'][0]['data']['datas']
    handle_jishu(data3,stat_date)


    # 第四个查询
    page.ele('@text()=估值').click()
    time.sleep(3)
    res4 = page.listen.wait()
    data4 = res4.response.body['data']['answer'][0]['txt'][0]['content']['components'][0]['data']['datas']

    handle_guzhi(data4,stat_date)

    # 第五个查询

    page.ele('@text()=资产负债').click()
    time.sleep(3)
    res5 = page.listen.wait()
    data5 = res5.response.body['data']['answer'][0]['txt'][0]['content']['components'][0]['data']['datas']
    handle_guzhi(data5, stat_date)


    # 第六个查询 -- 利润
    page.ele('@text()=利润').click()
    time.sleep(3)
    res6 = page.listen.wait()
    data6 = res6.response.body['data']['answer'][0]['txt'][0]['content']['components'][0]['data']['datas']

    handle_guzhi(data6, stat_date)

    print('查询完成，导出excel')

except ElementNotFoundError:
    print('程序异常退出')
# page.quit()
# 打印响应的JSON数据[0]
